package com.cqut.familiarity.controller;


import com.cqut.familiarity.entity.User;
import com.cqut.familiarity.entity.vo.ResultVO;
import com.cqut.familiarity.entity.vo.answerVO.AnswerVO;
import com.cqut.familiarity.entity.vo.userVO.Radar;
import com.cqut.familiarity.entity.vo.userVO.UserVO;
import com.cqut.familiarity.service.QuestionnaireService;
import com.cqut.familiarity.service.ScoreService;
import com.cqut.familiarity.service.UserService;
import com.cqut.familiarity.utils.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author atao
 * @since 2021-05-24
 */

@Api(description = "用户信息管理")
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private QuestionnaireService questionnaireService;


    /**
     * 根据用户id获取其基本信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据用户id获取其基本信息")
    @PostMapping("/get/information/by/{id}")
    public ResultEntity getUser(
            @ApiParam(name = "id", value = "用户id", required = true)
            @PathVariable String id
    ){
        User user = userService.getById(id);
        return ResultEntity.ok().data("user",user);
    }





    /**
     * 根据用户id修改其个人信息
     * @param userVO
     * @return
     */
    @ApiOperation(value = "根据用户id修改其个人信息")
    @PostMapping("/update")
    public ResultEntity updateUser(
            @ApiParam(name = "user", value = "用户基本信息", required = true)
            @RequestBody UserVO userVO
    ){
        User user = userService.getById(userVO.getId());
        user.update(userVO);
        userService.updateById(user);
        return ResultEntity.ok().data("message","修改成功");
    }

    /**
     * 查询你已为多少人填写过问卷
     * @param id
     * @return
     */
    @ApiOperation(value = "查询你已为多少人填写过问卷")
    @PostMapping("/get/filled/number/{id}")
    public ResultEntity filled(
            @ApiParam(name = "id", value = "用户id", required = true)
            @PathVariable String id
    ){
        int num = userService.filledNumber(id);
        return ResultEntity.ok().data("num",num);
    }

    /**
     * 查询以有多少人为你填写过问卷
     * @param id
     * @return
     */
    @ApiOperation(value = "查询以有多少人为你填写过问卷")
    @PostMapping("/get/fill/number/{id}")
    public ResultEntity fill(
            @ApiParam(name = "id", value = "用户id", required = true)
            @PathVariable String id
    ){
        int num = userService.fillNumber(id);
        return ResultEntity.ok().data("num",num);
    }


    /**
     * 好友列表
     * @param id
     * @return
     */
    @ApiOperation(value = "好友列表")
    @PostMapping("/friends/{id}")
    public ResultEntity listFriend(
            @ApiParam(name = "id",value = "用户ID",required = true)
          @PathVariable("id") String id
    ){
        List<UserVO> list = userService.listFriends(id);
        return ResultEntity.ok().data("message","查询成功").data("friends",list);
    }




    /**
     * 查看朋友对自己了解程度的排行榜
     * @param id
     * @return
     */
    @ApiOperation(value = "查看朋友对自己了解程度的排行榜")
    @GetMapping("/list/me/{id}")
    public ResultEntity listAboutMe(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @PathVariable("id") String id){
        List<ResultVO> list = questionnaireService.listAboutMe(id);
        Map<String,ResultVO> map = new HashMap<>();
        for (ResultVO resultVO:list){
            if (!map.containsKey(resultVO.getUserId())){
                map.put(resultVO.getUserId(),resultVO);
            }else{
                double res = (map.get(resultVO.getUserId()).getResult() + resultVO.getResult()) / 2;
                BigDecimal b = new BigDecimal(res);
                res = b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                resultVO.setResult(res);
                map.put(resultVO.getUserId(),resultVO);
            }
        }
        return ResultEntity.ok().data("map",map);
    }


    /**
     * 查看自己对朋友了解程度的排行榜
     * @param id
     * @return
     */
    @ApiOperation(value = "查看自己对朋友了解程度的排行榜")
    @GetMapping("/list/friend/{id}")
    public ResultEntity listAboutFriend(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @PathVariable("id") String id){
        List<ResultVO> list = questionnaireService.listAboutFriend(id);
        Map<String,ResultVO> map = new HashMap<>();
        for (ResultVO resultVO:list){
            if (!map.containsKey(resultVO.getUserId())){
                map.put(resultVO.getUserId(),resultVO);
            }else{
                double res = (map.get(resultVO.getUserId()).getResult() + resultVO.getResult()) / 2;
                BigDecimal b = new BigDecimal(res);
                res = b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                resultVO.setResult(res);
                map.put(resultVO.getUserId(),resultVO);
            }
        }
        return ResultEntity.ok().data("map",map);
    }

    /**
     * 查看自己对朋友了解程度的排行榜
     * @param id
     * @return
     */
    @ApiOperation(value = "查看自己对朋友了解程度的排行榜(墨迹测试)")
    @GetMapping("/list/friend2/{id}")
    public ResultEntity simiary(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @PathVariable("id") String id){
        List<ResultVO> list = questionnaireService.listAboutFriend2(id);
        if (list.get(0).getUserId().equals(id)){
            list = questionnaireService.listAboutFriend22(id);
        }
        System.out.println(id);
        System.out.println(list);
        Map<String,ResultVO> map = new HashMap<>();
        for (ResultVO resultVO:list){
            if (!map.containsKey(resultVO.getUserId())){
                map.put(resultVO.getUserId(),resultVO);
            }else{
                double res = (map.get(resultVO.getUserId()).getResult() + resultVO.getResult()) / 2;
                BigDecimal b = new BigDecimal(res);
                res = b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                resultVO.setResult(res);
                map.put(resultVO.getUserId(),resultVO);
            }
        }
        return ResultEntity.ok().data("map",map);
    }


    /**
     * 多用户雷达图
     * @return
     */
    @ApiOperation(value = "多用户雷达图测试")
    @PostMapping("/radars")
    public ResultEntity manyRadarsTest(
            @RequestBody Radar userIds
    ){
        double[] radar = null;

        String[][] res = null;

        String id = userIds.getId();

        List<String[][]> radars = new ArrayList<>();

        List<AnswerVO> user = userService.radar(id,id);

        if (user.size() != 0){
            radar = scoreService.score(user);
            res = new String[5][2];
            res[0][0] = "神经质";
            res[0][1] = String.valueOf(radar[0]);
            res[1][0] = "外向性";
            res[1][1] = String.valueOf(radar[1]);
            res[2][0] = "开放性";
            res[2][1] = String.valueOf(radar[2]);
            res[3][0] = "尽责性";
            res[3][1] = String.valueOf(radar[3]);
            res[4][0] = "宜人性";
            res[4][1] = String.valueOf(radar[4]);
            radars.add(res);
        }else {
            res = new String[5][2];
            res[0][0] = "神经质";
            res[0][1] = String.valueOf(0);
            res[1][0] = "外向性";
            res[1][1] = String.valueOf(0);
            res[2][0] = "开放性";
            res[2][1] = String.valueOf(0);
            res[3][0] = "尽责性";
            res[3][1] = String.valueOf(0);
            res[4][0] = "宜人性";
            res[4][1] = String.valueOf(0);
            radars.add(res);
        }

        for (String selectId: userIds.getSelectId()){
            user = userService.radar(id, selectId);
            if (user.size() != 0) {
                radar = scoreService.score(user);
                res = new String[5][2];
                res[0][0] = "神经质";
                res[0][1] = String.valueOf(radar[0]);
                res[1][0] = "外向性";
                res[1][1] = String.valueOf(radar[1]);
                res[2][0] = "开放性";
                res[2][1] = String.valueOf(radar[2]);
                res[3][0] = "尽责性";
                res[3][1] = String.valueOf(radar[3]);
                res[4][0] = "宜人性";
                res[4][1] = String.valueOf(radar[4]);
                radars.add(res);
            }else {
                res = new String[5][2];
                res[0][0] = "神经质";
                res[0][1] = String.valueOf(0);
                res[1][0] = "外向性";
                res[1][1] = String.valueOf(0);
                res[2][0] = "开放性";
                res[2][1] = String.valueOf(0);
                res[3][0] = "尽责性";
                res[3][1] = String.valueOf(0);
                res[4][0] = "宜人性";
                res[4][1] = String.valueOf(0);
                radars.add(res);
            }
        }
        return ResultEntity.ok().data("radars",radars);
    }

}

